package com.blueocean_health.care.management.controller;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.domain.base.OrderRefundInfo;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.RefundPayDto;
import com.blueocean_health.care.management.domain.ordervo.ChareOrderVo;
import com.blueocean_health.care.management.domain.ordervo.RefundInfoVo;
import com.blueocean_health.care.management.em.FeeTypeEnum;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.em.OrderStatusEnum;
import com.blueocean_health.care.management.exception.OrderException;
import com.blueocean_health.care.management.service.IChareOrderService;
import com.blueocean_health.care.management.service.IOrderSelectService;
import com.blueocean_health.care.management.service.OrderInfoService;


@RestController
@RequestMapping(value = "web/fee")
public class ChargeOrderHandler {
	@Resource
	private IChareOrderService iChareOrderService;
	@Resource
	private IOrderSelectService iOrderSelectService;
	@Resource
	private OrderInfoService orderInfoService;
	@RequestMapping(value = "collect")
	//收款
	public SysResult receivables(@RequestBody ChareOrderVo chareo){
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hospitalId = courrentUser.getHospitalId();
		chareo.setHospitalId(hospitalId);
		Integer receivables = iChareOrderService.receivables(chareo,FeeTypeEnum._1);
		if(receivables==-1){
			return SysResult.error("您填写的关联收据编号已被关联，请复核后再填写");
		}
		if(receivables<1){
			return SysResult.error("插入失败");
		}
		return SysResult.ok();
	}
	@RequestMapping(value = "collectnew")
	//收款
	public SysResult receivablesa(@RequestBody ChareOrderVo chareo) throws Exception{
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hospitalId = courrentUser.getHospitalId();
		chareo.setHospitalId(hospitalId);
		chareo.setIsReceivables(true);
		int receivables = iChareOrderService.receivableVo(chareo);
		if(receivables==-1){
			return SysResult.error("收据编号重复录入，请复核后再填写");
		}
		if(receivables==-2){
			return SysResult.error("页面内容已过期，请刷新后再操作");
		}
		return SysResult.ok();
	}
	//续费
	@RequestMapping(value = "add")
	public SysResult renew(@RequestBody ChareOrderVo chareo){
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hospitalId = courrentUser.getHospitalId();
		chareo.setHospitalId(hospitalId);
		Integer receivables = iChareOrderService.receivables(chareo,FeeTypeEnum._2);
		if(receivables==-1){
			return SysResult.error("您填写的关联收据编号已被关联，请复核后再填写");
		}
		if(receivables<1){
			return SysResult.error("插入失败");
		}
		return SysResult.ok();
	}
	//获取续费信息
	@RequestMapping(value = "getRenew")
	public SysResult getRenew(@RequestBody ChareOrderVo chareo){
		Map<String, String> renew = iChareOrderService.getRenew(chareo);
		
		return SysResult.ok(renew);
		
	}
	//查询订单号
	@RequestMapping(value = "receipt")
	public SysResult selectreceipt(@RequestBody ChareOrderVo chareo){
		if(null==chareo){
			return SysResult.error("参数不能为空");
		}
		  List<String> selectreceipt = iChareOrderService.selectreceipt(chareo);
		return SysResult.ok(selectreceipt);
	}
	//结单
	@RequestMapping(value = "finishOrder")
	public SysResult finishOrder(@RequestBody RefundInfoVo refundInfoVo) throws  OrderException{
		if(null==refundInfoVo){
			return SysResult.error("参数不能为空");
		}
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hospitalId = courrentUser.getHospitalId();
		refundInfoVo.setHospitalId(hospitalId);
			if(Double.valueOf(refundInfoVo.getRefundActualAmount())==0){
		//iOrderSelectService.updateOrderInfoStatus(refundInfoVo.getId());
		orderInfoService.updateOrderInfoStatus(OrderStatusEnum._5.getKey(), Integer.parseInt(refundInfoVo.getId()));
		return SysResult.ok();
			}
		  Integer receivables = iChareOrderService.refundapply(refundInfoVo);
		  if(receivables==-1){
			  return SysResult.error("订单状态已变更，请刷新页面后再操作");
		  }
		orderInfoService.updateOrderInfoStatus(OrderStatusEnum._4.getKey(), Integer.parseInt(refundInfoVo.getId()));
		return SysResult.ok(receivables);
	}
	//获取退款申请接口
	@RequestMapping(value = "selectRefund")
		public SysResult selectRefund(@RequestBody RefundInfoVo refundInfoVo) {
			if(null==refundInfoVo){
				return SysResult.error("参数不能为空");
			}
			List<OrderRefundInfo> selectRefund = iChareOrderService.selectRefund(refundInfoVo.getOrderId());
//			Map<String, OrderRefundInfo> map=new HashMap<String, OrderRefundInfo>();
//			map.put("refund", selectRefund.get(0));
			OrderRefundInfo orderRefundInfo=null;
			if(selectRefund!=null&&selectRefund.size()>0){
				 orderRefundInfo = selectRefund.get(0);
			}
			return SysResult.ok(orderRefundInfo);
		}
	//提交退款审批接口	
	@RequestMapping(value = "submitRefund")
	public SysResult submitRefund(@RequestBody RefundInfoVo refundInfoVo) throws OrderException{
		Integer submitRefund = iChareOrderService.submitRefund(refundInfoVo);
		 if(submitRefund==-1){
			  return SysResult.error("订单状态已变更，请刷新页面后再操作");
		  }
		if(submitRefund<1){
			return SysResult.error( "提交失败 请重试");
		}
		return SysResult.ok();
		
	}
	//获取退款金额接口
	@RequestMapping(value = "selectRefundpay")
	public SysResult selectRefundpay(@RequestBody RefundInfoVo refundInfoVo){
		RefundPayDto selectRefundpay = iChareOrderService.selectRefundpay(refundInfoVo);
		return SysResult.ok(selectRefundpay);
	}
	//清空退款信息
	@RequestMapping(value = "emptyChare")
	public SysResult emptyChare(@RequestBody ChareOrderVo chareo){
		iChareOrderService.emptyChare(chareo);
		return SysResult.ok();
	}
}
